package com.yh.csx.bsf.flow.engine;

import com.yh.csx.bsf.flow.core.base.FlowException;
import com.yh.csx.bsf.flow.core.base.Model;
import com.yh.csx.bsf.flow.core.base.Variable;
import com.yh.csx.bsf.flow.engine.api.UserApi;
import lombok.val;
import lombok.var;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: chejiangyi
 * @version: 2019-11-05 10:27
 * 表单权限换算
 **/
public class FormPermissionProvider {
    /**
     * 根据权限过滤字段 (隐藏)
     * @param variables
     * @return
     */
    public HashMap<String,String> filterVariables(HashMap<String,String> variables,Map<String,Model.PermissionEnum> permission){
        if(permission!=null){
            permission.forEach((k,v)->{
                if(v.Value == Model.PermissionEnum.hide.Value) {
                    var variable = variables.get(k);
                    if (variable != null) {
                        variables.remove(k);
                    }
                }
            });
        }
        return variables;
    }
    /**
     * 根据权限校验字段 （可写）
     */
    public HashMap<String,String> checkVariables(HashMap<String,String> variables,Map<String,Model.PermissionEnum> permission){
        if(variables!=null){
            variables.forEach((k,v)->{
                var permissionEnum = permission.get(k);
                if(permissionEnum.Value!=Model.PermissionEnum.write.Value){
                    throw new FlowException("变量key:"+k+"没有写权限");
                }
            });
        }
        return variables;
    }
    /**
     * 权限映射
     * @param formPermissions
     * @param variables
     * @param userno
     * @param node
     * @return
     */
    public Map<String,Model.PermissionEnum> permissionMap(int flowid, List<Model.FormPermission> formPermissions, List<Variable> variables, String userno, String node){
        val flowVariables = new HashMap<String,Variable>();
        variables.forEach(c->{
            flowVariables.put(c.getKey(),c);
        });
        List<Model.FormPermission> nodePermission = new ArrayList<>();
        if(formPermissions!=null){
            nodePermission = formPermissions.stream().filter(c->c.getNode().equals(node)).collect(Collectors.toList());
        }
        val temp = new ArrayList<Model.FormPermission>();
        if(nodePermission!=null){
            nodePermission.forEach(c->{
                if(c.getChooseUsers()!=null){
                   var usernos = UserApi.getUserNos(flowid,0,flowVariables,c.getChooseUsers());
                   if(usernos.contains(userno)){
                       temp.add(c);
                   }
                }
            });
        }
        //读>写>隐藏
        val permission = new HashMap<String,Model.PermissionEnum>();
        if(temp!=null){
            val hide = temp.stream().filter(c->c.getPermissionEnum()== Model.PermissionEnum.hide).collect(Collectors.toList());
            if(hide!=null){
                hide.forEach(c->{
                    permission.put(c.getVariableKey(), Model.PermissionEnum.hide);
                });
            }
            val write = temp.stream().filter(c->c.getPermissionEnum()== Model.PermissionEnum.write).collect(Collectors.toList());
            if(write!=null){
                write.forEach(c->{
                    permission.put(c.getVariableKey(), Model.PermissionEnum.write);
                });
            }
            val read = temp.stream().filter(c->c.getPermissionEnum()== Model.PermissionEnum.read).collect(Collectors.toList());
            if(read!=null){
                read.forEach(c->{
                    permission.put(c.getVariableKey(), Model.PermissionEnum.read);
                });
            }
        }
        return permission;
    }
}
